/*
 * @Author: Wilbur
 * @Date: 2019-01-08 14:08:47
 * @Last Modified by: Wilbur
 * @Last Modified time: 2019-03-29 16:11:01
 */

<template>
    <div class="container member-list-container">
        <div class="content-wrapper">
            <el-row
                class="search-wrapper"
                :gutter="20">
                <el-form
                    label-position="left"
                    label-width="84px"
                    size="small">
                    <el-col
                        class="filter-item"
                        :md="{ span: 24 }"
                        :sm="{ span: 24}">
                        <el-form-item label="资源筛选">
                            <el-checkbox-group
                                @change="fnChangeTypeList"
                                v-model="checkList">
                                <el-checkbox
                                    :label="item.type_no"
                                    v-for="(item, index) in dataTypeList"
                                    :value="item.type_no"
                                    :key="index">{{ item.type_name }}</el-checkbox>
                            </el-checkbox-group>
                        </el-form-item>
                    </el-col>
                    <el-col
                        class="filter-item"
                        :md="{ span: 6 }"
                        :sm="{ span: 12 }">
                        <el-form-item label="状态">
                            <el-select
                                v-model="filterData.bid_status"
                                @change="getTableData(1)"
                                placeholder="请选择">
                                <el-option
                                    label="全部"
                                    :value="0">全部</el-option>
                                <el-option
                                    label="未中标"
                                    :value="2">未中标</el-option>
                                <el-option
                                    label="已中标"
                                    :value="1">已中标</el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col
                        class="filter-item"
                        :md="{ span: 6 }"
                        :sm="{ span: 12 }">
                        <el-form-item label="">
                            <el-input
                                v-model="filterData.phone"
                                placeholder="请输入手机" />
                        </el-form-item>
                    </el-col>

                    <el-col
                        class="search-btn-wrapper"
                        :md="{ span: 6 }"
                        :sm="{ span: 12 }">
                        <el-button
                            size="small"
                            type="primary"
                            @click="getTableData(1)">搜索</el-button>
                    </el-col>
                </el-form>
            </el-row>
            <div class="table-wrapper">
                <el-table
                    :data="tableData"
                    height="100%"
                    @sort-change="handleSortChanged"
                    @selection-change="handleSelectionChange"
                    style="width: 100%;">
                    <el-table-column
                        type="selection"
                        width="40" />
                    <el-table-column
                        fixed="left"
                        header-align="left"
                        :width="50"
                        label="序号">
                        <template slot-scope="scope">
                            <div class="table-index">
                                {{ scope.$index + 1 }}
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        fixed="left"
                        :formatter="emptyFormatter"
                        prop="supply.phone"
                        min-width="110"
                        label="手机号" />

                    <el-table-column
                        prop="school[0].name"
                        :formatter="emptyFormatter"
                        :width="150"
                        show-overflow-tooltip
                        label="地区">
                        <template slot-scope="scope">
                            <template>{{ fnComputed(scope.row.province, 'name') }}</template>
                        </template>
                    </el-table-column>

                    <el-table-column
                        :width="150"
                        prop="accept_time"
                        sortable="custom"
                        label="竞标时间">
                        <template slot-scope="scope">
                            {{ scope.row.accept_time*1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="资源数量">
                        <template slot-scope="scope">
                            {{ scope.row.resource.length }}
                        </template>
                    </el-table-column>

                    <el-table-column
                        :formatter="emptyFormatter"
                        show-overflow-tooltip
                        :width="150"
                        label="资源展示">
                        <template slot-scope="scope">
                            <template>{{ fnComputed(scope.row.resource, 'title') }}</template>
                        </template>
                    </el-table-column>

                    <el-table-column
                        prop="supply.bid_success_count"
                        :min-width="114"
                        sortable="custom"
                        label="中标次数" />

                    <el-table-column
                        prop="bid_price"
                        :min-width="114"
                        sortable="custom"
                        label="报价" />

                    <el-table-column
                        :formatter="emptyFormatter"
                        prop="bid_intro"
                        show-overflow-tooltip
                        :width="150"
                        label="竞标说明" />


                    <el-table-column
                        prop="subscribe_platform"
                        :min-width="140"
                        label="状态">
                        <template slot-scope="scope">
                            <template v-if="scope.row.status == 0">待中标</template>
                            <template v-if="scope.row.status == 1">未中标</template>
                            <template v-if="scope.row.status == 2">已中标</template>
                        </template>
                    </el-table-column>

                    <el-table-column
                        prop="status"
                        label="操作"
                        fixed="right"
                        width="150">
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                v-if="scope.row.status == 0"
                                @click="fnAcceptBid(bid_no, scope.row.supply_no)">
                                中标
                            </el-button>
                            <el-button
                                type="text"
                                disabled
                                v-if="scope.row.status != 0">
                                --
                            </el-button>
                        </template>

                    </el-table-column>
                </el-table>
            </div>
            <div class="pagination-wrapper clearfix">
                <el-button
                    size="mini"
                    :disabled="selectedList.length == 0 ? true : false"
                    @click="fnMassAcceptBid">批量中标</el-button>
                <span class="tips">中标之后会自动给中标用户发送模板消息</span>
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getTableData" />
            </div>
        </div>
    </div>
</template>

<script>
let sortFieldMap = {
    accept_time: 0,
    bid_success_count: 1,
    bid_price: 2
};

let sortTypeMap = {
    'ascending': 1,
    'descending': 0
};

export default {
    data() {
        return {
            sortData: {
                field: 0,
                by: 0
            },
            checkList: [],
            authResult: {},
            tableData: [],
            filterData: {
                bid_status: 0,
                phone: ''
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            transformIds: [],
            bid_no: '',
            selectedList: [],
            dataTypeList: []
        };
    },
    created() {
        this.authResult = this.$route.meta.authResult;
        this.bid_no = this.$route.query.bid_no ? this.$route.query.bid_no : '';

        console.log(this.bid_no);
        this.$store.commit('SET_HEADER', [
            {
                title: "广告系统",
            },
            {
                title: "赞助列表"
            },
            {
                title: '报名人员'
            }
        ]);
        this.fnGetTypeList();
        this.getTableData();
    },
    methods: {
        fnChangeTypeList() {
            console.log(this.checkList);
            this.getTableData();
        },
        fnMassAcceptBid() {
            this.fnAcceptBid(this.bid_no, this.selectedList.toString());
        },
        handleSelectionChange(val) {

            this.selectedList = [];
            for(let i = 0; i < val.length; i++) {
                this.selectedList.push(val[i].supply_no);
            }
        },
        /**
         * 排序
         */
        handleSortChanged(data) {
            if (data.order) {
                console.log(data);
                this.sortData.field = sortFieldMap[data.prop];
                this.sortData.by = sortTypeMap[data.order];
            } else {
                this.sortData.field = 0;
                this.sortData.by = 1;
            }

            this.getTableData();
        },
        fnComputed(list, key) {
            let name = '';

            if(list) {
                for(let i = 0; i < list.length; i++) {
                    if(name) {
                        name += '/';
                    }
                    name += list[i][key];
                }
            }else{
                name = '--';
            }
            return name;

        },
        /**
         * 获取表格数据
         */
        getTableData(page) {

            this.$post('/wei4/bid_user_bid%5Cget_list', {
                page_id: page || this.page.now_page,
                bid_no: this.bid_no,
                bid_status: this.filterData.bid_status,
                resource_type_nos: this.checkList.toString(),
                phone: this.filterData.phone,
                sort_field: this.sortData.field,
                sort_type: this.sortData.by
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page = resp.data.page;
                }
            });
        },
        fnGetTypeList() {
            this.$post('/wei4/resource_type%5Cget_simple_list', {}, resp => {
                if (resp.code == 1) {
                    this.dataTypeList = resp.data.list;
                }
            });
        },
        fnAcceptBid(bid_no, supply_no) {
            this.$post('/wei4/bid_user_bid%5Cbid_success', {
                bid_no: bid_no,
                supply_no: supply_no
            }, resp => {
                if (resp.code == 1) {
                    this.getTableData(this.page.now_page);
                    this.$message({
                        type: 'success',
                        message: '操作成功'
                    });
                }else{
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },
        /**
         * 列表渲染辅助函数：列表项为空处理
         */
        emptyFormatter(row, column, val) {
            if (!val) {
                return '--';
            }
            return val;
        },
    }
};
</script>

<style lang="less" scoped>
.container {
    display: flex;
    flex-direction: column;
    width: 100%;
}

.content-wrapper {
    flex: 1;
    display: flex;
    flex-direction: column;
    margin: 15px;
    overflow: hidden;
}
.search-wrapper {
    padding: 15px;
    background-color: #fff;
}
.filter-item {
    margin-bottom: 15px;
    .el-form-item {
        margin-bottom: 0;
    }
    .el-autocomplete {
        width: 100%;
    }
}

.table-wrapper {
    flex: 1;
    background-color: #fff;
    overflow: hidden;
    .el-button {
        padding: 0;
    }

    .btn-delete {
        color: #fd4d32;
        &.is-disabled {
            color: #c0c4cc;
            cursor: not-allowed;
        }
    }
}
.pagination-wrapper {
    padding-top: 10px;
    .tips{
        color: #606266;
        font-size: 12px;
    }
    .el-pagination {
        float: right;
        padding: 0;
        background-color: transparent;
    }
}
</style>